<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;
use think\Request;

class Member extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $page  = request()->param('page', '1');
        $limit = request()->param('limit', 10);
        $grade = request()->param('grade', '');
        $xueyuan = request()->param('xueyuan', '');
        $keyword = request()->param('keywords', '');
        // 总数
        $where = [];
        if ($grade){
            $where[]=['grade','=',$grade];
        }
        if ($xueyuan){
            $where[]=['xueyuan','=',$xueyuan];
        }
        if ($keyword){
            $where[]=['realname|user_name','like','%'.$keyword];
        }
        $total = Db::name("Member")->where($where)->count();
        // 搜索条件

        $list = Db::name("Member")->where($where)->page($page, $limit)->order("uid desc")->select();
        foreach ($list as $key => $value) {
            $list[$key]['create_time'] = date('Y-m-d H:i', $value['create_time']);
            $list[$key]['class'] = Db::name("Banji")->where(['id'=>$value['class']])->value('title');
        }
        $data = [
            'total'  => $total,
            'list'   => $list,
            'grade'=>model("Banji")::TYPE,
            'xueyuan'=>Db::name("Yuan")->order("id desc")->column('id,title')
        ];
        return $this->ok($data);
    }



    public function edit_password()
    {
        $uid=request()->param("uid",'0','intval');
        $password=request()->param("password");
        if (empty($password)){
            return $this->fail('-1','请填写密码');
        }
        $password=md5($password);
        $res=model("Member")->where(['uid'=>$uid])->update(['password'=>$password]);
        if ($res){
            return $this->ok('修改密码成功');
        }else{
            return $this->fail('修改密码失败');
        }
    }

    //
    public function save()
    {

        $uid=request()->param("uid",'0','intval');
        $param=request()->only(['mobile','zhuanye','sex','xueyuan','class','grade','jinji_chat','home_mobile','fdy','house_id','sushe_id','realname']);

        if (empty($uid)){
            $param['password']=md5(123456);
            $code=rand(10, 99);
            $param['student_no']=date("YmdHis").$code;
            $param['create_time']=time();
            $res=Db::name("Member")->insert($param);
            $title="学生信息录入";
        }else{
            $res=Db::name("Member")->where(['uid'=>$uid])->update($param);
            $title="修改学生信息";
        }

        if ($res){
            return $this->ok($title.'成功');
        }else{
            return $this->fail($title.'失败');
        }
    }

    public function detail()
    {
        $uid=request()->param("uid",'0','intval');
        $detail=Db::name("Member")->where(['uid'=>$uid])->find();
        $data=[
            'xueyuan'=>Db::name("Yuan")->order("id desc")->column('id,title'),
            'grade'=>model("Banji")::TYPE,
            'detail'=>$detail,
            'fdy'=>Db::name('Admin')->where(['user_type'=>2])->column('uid,realname'),
            'house'=>Db::name("House")->order("id desc")->column('id,title'),
            'banji'=>Db::name("Banji")->order("id desc")->column("id,title"),
            'room'=>Db::name("Room")->order("id desc")->select(),
            ];



        return $this->ok($data);
    }


    //选择班级
    public function chose_banji()
    {
        $grade=request()->param("grade",'0','intval');
        $level=request()->param("level",'0','intval');//年级
        $where=[];
        $where[]=['grade','=',$grade];
        $where[]=['level','=',$level];
        $banji=Db::name('Banji')->order('id desc')->where($where)->column('id,title');
        return $this->ok($banji);
    }

    //获取宿舍
    public function chose_room()
    {
        $house_id=request()->param("house_id",'0','intval');
        $level=request()->param("level",'0','intval');//年级
        $room=Db::name("Room")->where(['house_id'=>$house_id])->select();
        if (!empty($room)){
            foreach ($room as $key=>$val){
                $res_bed=$val['count']-$val['has_people'];

                $room[$key]['title']=$val['title']."剩余床位(".$res_bed.")";
                $room[$key]['res_bed']=$res_bed;
            }
        }

        return $this->ok($room);
    }
}
